import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../service/http.service';
import {ActivatedRoute, Params} from '@angular/router';
import 'rxjs/add/operator/switchMap';
import {Comment} from '../../module/comment';
import {Cookie} from 'ng2-cookies/ng2-cookies';

@Component({
  selector: 'app-comment',
  templateUrl: './comment.component.html',
  styleUrls: ['./comment.component.css']
})
export class CommentComponent implements OnInit {

  commentList: Comment[];
  commentText: string;
  commentCount = 0;

  constructor(private route: ActivatedRoute,
              private httpService: HttpService) {
  }

  ngOnInit() {
    this.httpService.getCommentList(0, 0).then(r => {
      this.commentList = r;
      this.commentCount = r.length;
    });
  }

  submitComment() {
    if (this.commentText == null) {
      alert('评论不能为空!');
      return;
    }
    const token = Cookie.get('token');
    if (token != null) {
      this.httpService.postComment(token, 0, this.commentText).then(s => {
        alert(s);
        location.reload();
      });
    } else {
      alert('请先登录!');
    }
  }


}
